<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 19px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加属性</el-breadcrumb-item>
    </el-breadcrumb>
  <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="属性模板" prop="check">
          <el-select v-model="ruleForm.templateId" placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <template>
          <el-radio-group v-model="ruleForm.type">
            <div class="right">
              <el-tooltip class="item" effect="dark" content="Right Center 提示文字" placement="right">
                <el-radio  :label="0">非销售属性<i class="el-icon-info"></i></el-radio>
              </el-tooltip>
            </div>
            <div class="right">
              <el-tooltip class="item" effect="dark" content="Right Center 提示文字" placement="right">
                <el-radio :label="1">销售属性<i class="el-icon-info"></i></el-radio>
              </el-tooltip>
            </div>
          </el-radio-group>
        </template>
      </el-form-item>

      <el-form-item label="输入类型" prop="inputType">
        <template>
          <el-radio-group v-model="ruleForm.inputType">
            <el-radio :label="0">手动录入</el-radio>
            <el-radio :label="1">单选</el-radio>
            <el-radio :label="2">多选</el-radio>
            <el-radio :label="3">单选（下拉列表）</el-radio>
            <el-radio :label="4">多选（下拉列表）</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item label="备选列表" prop="unit">
        <el-input  v-model="ruleForm.unit"></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input  v-model="ruleForm.valueList"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input  v-model="ruleForm.sort"></el-input>
      </el-form-item>



      <el-form-item >
        <el-button  type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>



  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [],
      value: '',
      ruleForm: {
        templateId: '',
        name: '',
        description: '',
        type: 0,
        inputType: 0,
        valueList: '',
        unit: '',
        sort: ''
      },
      rules: {
        templateId: [
          {required: true, message: '请选择属性模板', trigger: 'change'},
        ],
        name: [
          {required: true, message: '请输入属性名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9090/attributes/add-new';
          console.log('url='+url);
          // this.axios.post(url,this.ruleForm)
          //方法将一个 JavaScript 对象或值转换为 JSON 字符串，如果指定了一个 replacer 函数，
          // 则可以选择性地替换值，或者指定的 replacer 是数组，则可选择性地仅包含数组指定的属性。
          let formDate = this.qs.stringify(this.ruleForm)
          console.log('formDate='+formDate);
          this.axios.post(url,formDate).then((response) => {
            let responseBody = response.data;
            console.log(responseBody)
            if(responseBody.state==20000){
              //成功
              this.$message({
                message: responseBody.message,
                type: "success"
              });
              this.resetForm(formName);
            }else {
              //失败
              this.$message.error(responseBody.message)
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadAttributeTemplateList(){
      let url = "http://localhost:9090/attribute-templates";
      console.log('url=' + url);
      this.axios.get(url).then((response) =>{
        let responseBody = response.data;
        this.options = responseBody.data;
      })
    },
  },

  mounted() {
    console.log("mounted")
    this.loadAttributeTemplateList();
  }
}
</script>
<style>
.right {
  float: right;
  width: 150px;
}
</style>